<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- 页面标题 -->
    <title>computed属性练习</title>
  </head>
  <body>
    <!-- 应用容器，Vue实例将挂载到此元素 -->
    <div id="app">
      姓：<input type="text" v-model="firstName" /><br />
      名：<input type="text" v-model="lastName" /><br />
      姓名：<span>{{fullName}}</span>
    </div>
    <!-- 引入Vue.js库 -->
    <script src="./js/vue.js"></script>
    <script>
      // 创建Vue实例
      const vm = new Vue({
        data: {
          // 用户名首字母和末尾字母的初始状态
          firstName: "",
          lastName: "",
        },
        computed: {
          // 定义fullName计算属性，用于获取和设置完整的姓名
          fullName: {
            get(fullName) {
              // 获取fullName的值时，拼接firstName和lastName
              return this.firstName + "" + this.lastName;
            },
            set(val) {
              // 设置fullName的值时，分割字符串并更新firstName和lastName
              const [firstName, lastName] = val.split("");
              this.firstName = firstName;
              this.lastName = lastName;
            },
          },
        },
      });
      // 将Vue实例挂载到#app元素上
      vm.$mount("#app");
    </script>
  </body>
</html>
